//
//  WLCToastView.m
//  Training
//
//  Created by lichunwang on 16/12/22.
//  Copyright © 2016年 springcome. All rights reserved.
//

#import "WLCToastView.h"

@interface WLCToastView ()

@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

@implementation WLCToastView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.textLabel.layer.cornerRadius = 6;
    self.textLabel.layer.masksToBounds = YES;
}

//2017-01-16 20:10:08.407 Training[17928:2668357] top window = <_UIAlertShimPresentingViewController: 0x12ed6f0d0>
//2017-01-16 20:10:08.408 Training[17928:2668357] dest window = <UIInputWindowController: 0x12f894200>
//2017-01-16 20:10:08.408 Training[17928:2668357] dest window = <UITabBarController: 0x12ee7b1b0>
//2017-01-16 20:10:08.410 Training[17928:2668357] dest window = <UINavigationController: 0x12ed0efb0>
//2017-01-16 20:10:08.411 Training[17928:2668357] dest window = <UIInputWindowController: 0x12f894200>
// UIInputWindowController左滑删除的时候会出现

+ (UIWindow *)topWindow
{
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    Class class = NSClassFromString(@"_UIAlertShimPresentingViewController");
    if ([topWindow.rootViewController isKindOfClass:class]) {
        NSArray *windows = [UIApplication sharedApplication].windows;
        for (NSInteger i = windows.count - 1; i >= 0; i--) {
            UIWindow *tmpWindow = windows[i];
            if ([tmpWindow.rootViewController isKindOfClass:NSClassFromString(@"UITabBarController")] ||
                [tmpWindow.rootViewController isKindOfClass:NSClassFromString(@"UINavigationController")]) {
                topWindow = tmpWindow;
                break;
            }
        }
    }
    
    return topWindow;
}

// 这里的window好多坑，根据具体业务做变通
+ (void)toastWithMessage:(NSString *)message
{
    UIWindow *topWindow = [self topWindow];
    
    WLCToastView *toastView = (WLCToastView *)[UIView viewWithNib:@"WLCToastView" owner:nil];
    toastView.frame = topWindow.bounds;
    
    UILabel *label = toastView.textLabel;
    label.text = message;
    label.alpha = 0;
    CGRect rect = [message boundingRectWithSize: CGSizeMake(CGFLOAT_MAX, 35) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    label.width = rect.size.width + 20;
    if (label.width > toastView.width - 40) {
        label.width = toastView.width - 40;
    }
    if (label.width <= 120) {
        label.width = 120;
    }
    label.center = CGPointMake(toastView.width/2.0, label.center.y);
    
    [topWindow addSubview:toastView];
    [UIView animateWithDuration:0.3 animations:^{
        label.alpha = 1;
    } completion:^(BOOL finished) {
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW,
                                             (int64_t)(1.5 * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.3 animations:^{
                label.alpha = 0;
            } completion:^(BOOL finished) {
                [toastView removeFromSuperview];
            }];
        });
    }];
}

+ (void)toastWithMessage:(NSString *)message error:(NSError *)error
{
    NSDictionary *userInfo = error.userInfo;
    NSString *errorMessage = userInfo[@"message"];
    if (errorMessage.length > 0) {
        message = [NSString stringWithFormat:@"%@", errorMessage];
    }
    
    // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
        [self toastWithMessage:message];
    //                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             });
}

@end
